package org.chen.aao.app.controller;

import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.chen.aao.app.service.AppVersionService;
import org.chen.aao.common.app.dto.AppVersionCheckRequest;
import org.chen.aao.common.app.dto.AppVersionCheckResponse;
import org.chen.aao.common.response.Result;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * APP版本更新控制器
 */
@Tag(name = "APP版本管理")
@RestController
@RequestMapping("/version")
@RequiredArgsConstructor
public class AppVersionController {
    
    private final AppVersionService appVersionService;
    
    /**
     * 检查APP版本更新
     */
    @Schema(name = "检查APP版本更新")
    @PostMapping("/check-update")
    public Result<AppVersionCheckResponse> checkVersion(@Validated @RequestBody AppVersionCheckRequest request) {
        AppVersionCheckResponse response = appVersionService.checkVersion(request);
        return Result.success(Objects.requireNonNullElseGet(response, () -> createNoUpdateResponse(request.getVersion())));
    }
    
    /**
     * 创建无需更新的响应
     */
    private AppVersionCheckResponse createNoUpdateResponse(String currentVersion) {
        AppVersionCheckResponse response = new AppVersionCheckResponse();
        response.setLatestVersion(currentVersion);
        response.setMinimumVersion(currentVersion);
        response.setUpdateUrl("");
        response.setReleaseNotes("当前已是最新版本");
        response.setForceUpdate(false);
        return response;
    }
}